/**
 * TODO
 */
package com.bci.pwtz.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.DydlMapper;
import com.bci.pwtz.mysql.model.Dydl;
import com.bci.pwtz.service.BusinessRefService;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.DydlService;

/**
 * TODO
 * 
 * @author ICLNetwork Team
 * @version 3.0
 */
@Service
public class DydlServiceImpl implements DydlService{
    private DydlMapper objMapper;
    
    private CompanyService companyService;
    
    private BusinessRefService businessRefService;
    
    private static final Logger _logger = Logger.getLogger(PdbyqServiceImpl.class);

    public DydlMapper getObjMapper() {
        return objMapper;
    }

    public void setObjMapper(DydlMapper objMapper) {
        this.objMapper = objMapper;
    }

    public CompanyService getCompanyService() {
        return companyService;
    }

    public void setCompanyService(CompanyService companyService) {
        this.companyService = companyService;
    }

   public BusinessRefService getBusinessRefService()
    {
        return businessRefService;
    }

    public void setBusinessRefService(BusinessRefService businessRefService)
    {
        this.businessRefService = businessRefService;
    }

/**
    * 新增低压电缆信息
    */
    @Override
    public Dydl add(Dydl dydl) throws PwtzDatabaseException {
        if (_logger.isDebugEnabled())
        {
            _logger.debug("add:dydl=" + dydl.toString());
        }
        if (dydl == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("add,dydl is null");
            }
            return null;
        }else{
            /*UUID uuid = UUID.randomUUID();
            dydl.setDeviceid(uuid.toString());*/
            dydl.setStatus(1);
            dydl.setLastmodifiedtime(new Date());
            dydl.setCreatedate(new Date());
        }
        try
        {
            objMapper.insertSelective(dydl);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return dydl;
    }

    /**
     * 删除低压电缆信息
     */
    @Override
    public int delete(String dydlId) throws PwtzDatabaseException, CanNotDeleteException {
        int result=0;
        if (_logger.isDebugEnabled())
        {
            _logger.debug("delete:dydlId=" + dydlId);
        }
        try
        {
            boolean bl = businessRefService.canDelete("T_DYDL", dydlId);
            if(!bl){
                throw new CanNotDeleteException();
            }
            result = objMapper.deleteById(dydlId,new Date());
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    /**
     * 修改低压电缆信息
     */
    @Override
    public int update(Dydl dydl) throws PwtzDatabaseException {
        int result=0;
        if (_logger.isDebugEnabled())
        {
            _logger.debug("update:dydl=" + dydl.toString());
        }
        if (dydl == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("update,dydl is null");
            }
            return 0;
        }else{
            dydl.setLastmodifiedtime(new Date());
        }
        try
        {
            result = objMapper.updateByPrimaryKeySelective(dydl);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    /**
     * load查看低压电缆信息
     */
    @Override
    public Dydl load(String dydlId) throws PwtzDatabaseException {
        if (_logger.isInfoEnabled())
        {
            _logger.info("load:dydlId=" + dydlId);
        }
        Dydl dydl = null;
        try
        {
            dydl = objMapper.selectByPrimaryKey(dydlId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (dydl != null)
        {
            _logger.debug("load:result dydlId=" + dydl.toString());
        }
        return dydl;
    }
    /**
     * list查询低压电缆信息
     */
    @Override
    public PaginationList<Dydl> findDydl(String devicename, Long companyid, int portalType,String[] medLineCodes,String lineCode, String beginTime,
            String endTime, int pageSize, int pageNo) throws PwtzDatabaseException {
      PaginationList<Dydl> paginationList = new PaginationList<Dydl>();
        
        if (_logger.isInfoEnabled())
        {
            _logger.info("findDybx:|devicename=" + devicename+"|beginTime=" + beginTime+"|endTime=" + endTime+"|companyid="+companyid+"|portalType="+portalType+"|medLineCodes="+PwtzUtils.parseCharacterArrayToString(medLineCodes)+"|lineCode="+lineCode+"|pageSize="+pageSize+"|pageNo="+pageNo);
        }
        List<Dydl> list = null;
        int beginIndex = pageSize * pageNo - pageSize;
        String companyidStr = "";
        String[] companyids=null;
        if(portalType==PwtzConstants.PORTAL_TYPE_WEB){
            companyidStr = companyService.findChildrensId(companyid,null);
            if(companyidStr!=null && companyidStr.length()>0){
                companyidStr= companyidStr.substring(0,companyidStr.length()-1);
                companyids =companyidStr.split(",");
            }
        }
        try
        {
            list = objMapper.selectDeviceList(devicename,companyid,companyids,portalType,medLineCodes,lineCode, beginTime,endTime,beginIndex, pageSize);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("findDydl:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage =1;
        if(pageSize!=0){
            try
            {
                totalCount = objMapper.selectDeviceCount(devicename,companyid,companyids,portalType,medLineCodes,lineCode, beginTime,endTime);
            }
            catch (DataAccessException e)
            {
                _logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        }else{
            pageNo=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }

}
